package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.UserBanquetInvitation;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBanquetInvitationDaoImpl extends TempDataObjectDao<UserBanquetInvitation> {

    public Map<String,UserBanquetInvitation> getUserBanquetInvitationList(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_banquet_invitation where userId=?";
        Map<String,UserBanquetInvitation> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);

            rs = call.executeQuery();
            while (rs.next()) {
                UserBanquetInvitation data = parse(rs);
                res.put(data.getiId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }

    @Override
    protected boolean addData(UserBanquetInvitation data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_banquet_invitation(iId,userId,`type`,info,overTime,isUsed,createTime,useTime) values(?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getiId());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getType());
            call.setString(i++, data.getInfo());
            call.setLong(i++, data.getOverTime());
            call.setBoolean(i++, data.isUsed());
            call.setLong(i++, data.getCreateTime());
            call.setLong(i++, data.getUseTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserBanquetInvitation data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_banquet_invitation SET isUsed=?,useTime = ? where iId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setBoolean(i++, data.isUsed());
            call.setLong(i++, data.getUseTime());
            call.setString(i++, data.getiId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBanquetInvitation parseData(ResultSet rs) throws SQLException {
        UserBanquetInvitation data = new UserBanquetInvitation();
        data.setiId(rs.getString("iId"));
        data.setUserId(rs.getLong("userId"));
        data.setType(rs.getInt("type"));
        data.setInfo(rs.getString("info"));
        data.setCreateTime(rs.getLong("createTime"));
        data.setOverTime(rs.getLong("overTime"));
        data.setUsed(rs.getBoolean("isUsed"));
        data.setUseTime(rs.getLong("useTime"));
        return data;
    }
}
